//
//  MKListViewController.m
//  ManKeCartoon
//
//  Created by dllo on 15/10/20.
//  Copyright © 2015年 ManKe. All rights reserved.
//

#import "MKListViewController.h"
#import "MKBaseHeader.h"

@interface MKListViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDataSource,UICollectionViewDelegate>

@end

@implementation MKListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-50, 24, 100, 30)];
    
//    [titleLabel setText:@"排行榜"];
    [titleLabel setTextAlignment:NSTextAlignmentCenter];
    titleLabel.text = self.Headname;
    titleLabel.textColor = [UIColor whiteColor];
    [self.navBarView addSubview:titleLabel];
    

    [self setUpBackButton];
    
    
    self.listTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, 580*HEIGHT)];
    self.listTableView.dataSource = self;
    self.listTableView.delegate = self;
    [self.view addSubview:self.listTableView];
    
    [MKAFNmodel getdataWithString:[NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Rank/rankDetail/rank_id/%@/timestamp/1/user_imei/866654028915780/user_qq/null/user_local_version/5.0.7/user_channel/2011",self.rank_id] block:^(id block) {
        
        
       // http://mobilev3.ac.qq.com/Rank/rankDetail/rank_id/11/timestamp/1/user_imei/868942026790232/user_qq/null/user_local_version/5.0.7/user_channel/2061
        
        
       // http://mobilev3.ac.qq.com/Rank/rankDetail/rank_id/%@/timestamp/1/user_imei/866654028915780/user_qq/null/user_local_version/5.0.7/user_channel/2011"
        
        self.listArray = [[NSMutableArray alloc]init];
        
        for (NSMutableDictionary *dic in [block objectForKey:@"data"]) {
            
            MKChartsModel *model = [[MKChartsModel alloc]init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.listArray addObject:model];
        

        }
        
        [self.listTableView reloadData];
   
    }];
    
    
    
    
    
    // Do any additional setup after loading the view.
}

#pragma mark - 创建一个返回按钮
- (void)setUpBackButton
{
    _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backButton setFrame:CGRectMake(10, 18, 30, 30)];
    [_backButton setImage:[UIImage imageNamed:@"iconfont-arrowleft.png"] forState:UIControlStateNormal];
    [_backButton addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.navBarView addSubview:_backButton];
}
#pragma mark - 返回按钮点击方法
- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
   
    
    return self.listArray.count - 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
   
    if (indexPath.row == 0) {
        
        static NSString *identifier = @"MKSupageTableViewCell";
        MKSubpageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[MKSubpageTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.delegat = self;
        cell.datasource = self;
        return cell;
        
    }else{
        static NSString *identifier = @"MKCharstTableViewCell";
        MKCharstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[MKCharstTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
       
        MKChartsModel*model = self.listArray[indexPath.row +2];
        
        cell.charstNumber.text = [NSString stringWithFormat:@"%ld",indexPath.row+3];
        
        [cell getcell:model];


        
        return cell;
    }
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 200;
    }else{
        return 140;
    }
    
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
{
    return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    MKSubpageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MKSubpageCollectionViewCell" forIndexPath:indexPath];
    
    MKChartsModel *model = self.listArray[indexPath.row];
   
    cell.backgroundColor = [UIColor whiteColor];
    [cell getcell:model];
    
    if (indexPath.row == 0) {
        cell.rankLabel.text = @"1";
        cell.rankLabel.backgroundColor = [UIColor redColor];
    }else if (indexPath.row == 1){
        cell.rankLabel.text = @"2";
         cell.rankLabel.backgroundColor = [UIColor redColor];
    }else if (indexPath.row == 2){
        cell.rankLabel.text = @"3";
         cell.rankLabel.backgroundColor = [UIColor redColor];
    }

   
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MKCartoonViewController *cartoonView = [[MKCartoonViewController alloc]init];
    MKChartsModel *model = self.listArray[indexPath.row+2];
    cartoonView.comic_id = model.comic_id;
    [self.navigationController pushViewController:cartoonView animated:YES];
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MKCartoonViewController *cartoonView = [[MKCartoonViewController alloc]init];
    MKChartsModel *model = self.listArray[indexPath.row];
    cartoonView.comic_id = model.comic_id;
    
    [self.navigationController pushViewController:cartoonView animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
